package com.huanhong.hik.util.callback;

import com.alibaba.fastjson.JSONObject;
import com.huanhong.hik.bean.Result;
import com.huanhong.hik.socket.SocketIoClient;
import com.huanhong.hik.util.HCNetSDK;
import com.sun.jna.Pointer;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class FRemoteCfgCallBackCardGet implements HCNetSDK.FRemoteConfigCallback {

    private String method;

    @Override
    public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData) {
        log.info("长连接回调获取数据,NET_SDK_CALLBACK_TYPE_STATUS:{}", dwType);
        switch (dwType) {
            case 0: //NET_SDK_CALLBACK_TYPE_STATUS
                HCNetSDK.REMOTECONFIGSTATUS_CARD struCfgStatus = new HCNetSDK.REMOTECONFIGSTATUS_CARD();
                struCfgStatus.write();
                Pointer pCfgStatus = struCfgStatus.getPointer();
                pCfgStatus.write(0, lpBuffer.getByteArray(0, struCfgStatus.size()), 0, struCfgStatus.size());
                struCfgStatus.read();

                int iStatus = 0;
                for (int i = 0; i < 4; i++) {
                    int ioffset = i * 8;
                    int iByte = struCfgStatus.byStatus[i] & 0xff;
                    iStatus = iStatus + (iByte << ioffset);
                }

                switch (iStatus) {
                    case 1000:// NET_SDK_CALLBACK_STATUS_SUCCESS
                        log.info("查询卡参数成功,dwStatus:{}", iStatus);
                        break;
                    case 1001:
                        log.info("正在查询卡参数中,dwStatus:{}", iStatus);
                        break;
                    case 1002:
                        int iErrorCode = 0;
                        for (int i = 0; i < 4; i++) {
                            int ioffset = i * 8;
                            int iByte = struCfgStatus.byErrorCode[i] & 0xff;
                            iErrorCode = iErrorCode + (iByte << ioffset);
                        }
                        log.info("查询卡参数失败, dwStatus:{},错误号:{}", iStatus, iErrorCode);
                        break;
                    default:
                        log.warn("无效状态");
                }
                break;
            case 2: //NET_SDK_CALLBACK_TYPE_DATA
                HCNetSDK.NET_DVR_CARD_CFG_V50 mStruCardInfo = new HCNetSDK.NET_DVR_CARD_CFG_V50();
                mStruCardInfo.write();
                Pointer pInfoV30 = mStruCardInfo.getPointer();
                pInfoV30.write(0, lpBuffer.getByteArray(0, mStruCardInfo.size()), 0, mStruCardInfo.size());
                mStruCardInfo.read();
                String cardNo = new String(mStruCardInfo.byCardNo).trim();
                try {
                    //姓名
                    String name = new String(mStruCardInfo.byName, "GBK").trim();
                    log.info("查询到的卡号,getCardNo:{},姓名:{}", cardNo,name);
                    if(null != SocketIoClient.socket){
                        JSONObject data = new JSONObject();
                        data.put("cardNo", cardNo);
                        data.put("name", name);
                        SocketIoClient.socket.emit(method, Result.success(data).toJsonString());
                    }

                } catch (UnsupportedEncodingException e1) {
                    log.error("UnsupportedEncodingException：", e1);
                }
                break;
            default:
                break;
        }
    }
}
